package hottargui.config;

import java.util.*; 
import hottargui.framework.*;
import hottargui.standard.*;


public class AlphaEndRoundState implements TarguiState {

	Game gameContext;
	
	public AlphaEndRoundState(Game game)
	{
		this.gameContext = game;
	}
	
	public boolean buy(int count, Position deploy) throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public State getState() {
		return State.endRound;
	}

	public PlayerColor getWinner() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public boolean move(Position from, Position to, int count) throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public void newGame() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public void rollDie() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public PlayerColor turnCard() throws IllegalActionException {
	    TurnSequenceStrategy turnSequence = gameContext.getGameObjectServer().getTurnSequenceStrategy();
	    GameObjectServer objServer =  gameContext.getGameObjectServer();
	    objServer.getGameData().setCurrentColor(turnSequence.getNextPlayer());
	    objServer.getGameData().endRound();
	    if (objServer.getGameData().getRoundsLeft() == 0) {
		gameContext.setState(gameContext.getFactory().getEndGameState(gameContext));
	    }	
	    else {
		gameContext.setState(gameContext.getFactory().getMoveState(gameContext));
	    }
	    
	    // Calculate revenue
	    Map<PlayerColor, Player> players = objServer.getGameData().getPlayers();
	    for (Tile tile : objServer.getGameData().getBoardConfiguration()) {
		if (players.containsKey(tile.getOwnerColor())) {
		  StandardPlayer owner = (StandardPlayer)players.get(tile.getOwnerColor());
		  owner.add(tile.getType().getEconomicValue());
		}
	    }
	    return objServer.getGameData().getCurrentColor();
	}
}
